﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Xml.Linq;

namespace BlueHarvestSnippetGenerator
{
    public static class Utilitys
    {
        private const string XamlExt = ".xaml";

        static public bool PageNav(this NavigationService data, PageName pages)
        {
            return data.Navigate(new Uri("Pages\\"+pages+XamlExt, UriKind.Relative));
        }

        static public bool TutorialNav(this NavigationService data, string tutorialPath)
        {
            return data.Navigate(new Uri(tutorialPath, UriKind.Relative));
        }

        public static bool TruthCount(int threshold, IEnumerable<bool> bools)
        {
            return bools.Count(b => b) >= threshold;
        }

        public static string FlagDescription(this object value)
        {

            try
            {
                FieldInfo fieldInfo = value.GetType().GetField(value.ToString());

                var attributes =(DescriptionAttribute[])fieldInfo.GetCustomAttributes
                                (typeof(DescriptionAttribute), false);   

                return ((attributes.Length > 0) ? attributes[0].Description : value.ToString());
            }
            catch (NullReferenceException nre)
            {
                return nre.Message;
            }
        }


        public static string TryGetElementValue(this XElement parentElement,XNamespace ns, string elementName)
        {
            var foundElement = parentElement.Element(ns+elementName);
            if (foundElement != null)
            {
                return foundElement.Value;
            }
            return "not found";

        }
        public static string TryGetElementValue(this XElement parentElement, string attributeName)
        {
            var foundElement = (string)parentElement.Attribute(attributeName) ?? (string)parentElement.Element(attributeName);
            if (foundElement != null)
            {
                return foundElement;
            }
            return "not found";

        }
       
        public static string TryGetElementValue(this XElement parentElement)
        {
            var foundElement = parentElement;
            if (foundElement != null)
            {
                return foundElement.Value;
            }
            return "not found";

        }

        
        public static DependencyObject GetDependencyObjectFromVisualTree(DependencyObject startObject, Type type)
        {
            DependencyObject parent = startObject;
            while (parent != null)
            {
                if (type.IsInstanceOfType(parent))
                    break;
                else
                    parent = VisualTreeHelper.GetParent(parent);
            }

            return parent;
        }


        public static IEnumerable<T> GetAllControls<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in GetAllControls<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

        
    }
    
}
